package com.lau.videoeffect

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.lau.videoeffect.camera.CameraActivity
import com.lau.videoeffect.mediaplayer.MediaPlayerActivity
import com.lau.videoeffect.multiplayer.MultiMediaPlayerActivity
import kotlinx.android.synthetic.main.launch_activity.*

class LaunchActivity : AppCompatActivity() {

    companion object {
        private const val PERMISSION_REQUEST_CAMERA = 0
        private const val PERMISSION_REQUEST_STORAGE = 1
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.launch_activity)
        checkPermissions()


        mBtnCamera.setOnClickListener {
            if (checkPermissions()) {
                startActivity(Intent(this@LaunchActivity, CameraActivity::class.java))
            }
        }

        mBtnMediaPlayer.setOnClickListener {
            if (checkPermissions()) {
                startActivity(Intent(this@LaunchActivity, MediaPlayerActivity::class.java))
            }
        }

        mBtnMultiMediaPlayer.setOnClickListener {
            if (checkPermissions()) {
                startActivity(Intent(this@LaunchActivity, MultiMediaPlayerActivity::class.java))
            }
        }
    }

    private fun checkPermissions(): Boolean {
        if (Build.VERSION.SDK_INT >= 23) {
            if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(arrayOf(Manifest.permission.CAMERA), PERMISSION_REQUEST_CAMERA)
                return false
            } else if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(
                    arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
                    PERMISSION_REQUEST_STORAGE
                )
                return false
            }
        }
        return true
    }
}